#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv){
  int i, status;
  pid_t pid;

  for(i=0; i<2; i++){
      if(argc < 3){
        printf("SINTAXIS: ./ejercicio6 gnome-calculator gedit FicheroN.txt...\n");
        exit(EXIT_FAILURE);
      }

      pid = fork();
      switch(pid){
        case -1: //Error
          perror("Fork error");
          printf("errno value = %d\n", errno);
          exit(EXIT_FAILURE);
        break;

        case 0: //Proceso hijo
          if(i == 0){
            if(openGnomeCalculator(argv[1]) == EXIT_FAILURE)
            {
              perror("Falla en la ejecucion exec");
              printf("errno value= %d\n", errno);
              exit(EXIT_FAILURE);
            }
          }
          else{
            if (openGedit(argv) == EXIT_FAILURE)
            {
              perror("exec");
              printf("errno value= %d\n", errno);
            }
          }
        break;

        default: //Proceso padre
        while(pid!=wait(&status));
        break;
      }
  }
}

int openGnomeCalculator(char* args){
  if(execlp(args,args,NULL) == -1){
    return(EXIT_FAILURE);
  }
  else{
    return(EXIT_SUCCESS);
  }
}

int openGedit(char** args){
  if(execvp(args[2], &args[2]) == -1){
    return (EXIT_FAILURE);
  }
  else{
    return (EXIT_SUCCESS);
  }
}
